# import json
# import requests
# import sys
# # def save_text_to_json(text):
# #     data = {
# #         'input': text
# #     }
# #     with open('input.json', 'w') as file:
# #         json.dump(data, file)
# # def get_chatglm_response():
# path = './package.json'
# with open(path, 'r', encoding='utf-8') as f:
#     data = json.load(f)
# if (data is None):
#     sys.exit(1)
# # downTime = data[0]['downTime']
# # with open('package.json', 'r') as file:
# #     data = json.load(file)
# input_text = data['input']
# history = data['history']
# max_length = data['max_length']
# top_p = data['top_p']
# temperature = data['temperature']
# # 以下是调用ChatGLM模型的代码
# ajax_url = "http://acv-gydn.baocloud.cn/acv-service/service/api/chatglm/v1/predict"
# headers = {
#     'Accept': 'application/json, text/javascript, */*; q=0.01',
#     'Accept-Encoding': 'gzip, deflate, br',
#     'Accept-Language': 'zh-CN,zh;q=0.9',
#     'Connection': 'keep-alive',
#     'Content-Length': '68',
#     'Content-Type': 'application/json',
#     'Host': 'acv-gydn.baocloud.cn',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
# }
#
#
#
# payload ={
#     'prompt': input_text,
#     'history': history,
#     'temperature': temperature,
#     'max_length': max_length,
#     'top_p': top_p
# }
#
# response = requests.post(ajax_url, json=payload).json()["response"]
# print(response)
# print('finish')
#
# import openai
# import os
# from langchain.llms import OpenAI
# from langchain.graphs.networkx_graph import KnowledgeTriple
# import networkx as nx
# import matplotlib.pyplot as plt
# from langchain.indexes import GraphIndexCreator
#
# # Knowledge graph
# kg = [
#     ('Apple', 'is', 'Company'),
#     ('Apple', 'created', 'iMac'),
#     ('Apple', 'created', 'iPhone'),
#     ('Apple', 'created', 'Apple Watch'),
#     ('Apple', 'created', 'Vision Pro'),
#
#     ('Apple', 'developed', 'macOS'),
#     ('Apple', 'developed', 'iOS'),
#     ('Apple', 'developed', 'watchOS'),
#
#     ('Apple', 'is located in', 'USA'),
#     ('Steve Jobs', 'co-founded', 'Apple'),
#     ('Steve Wozniak', 'co-founded', 'Apple'),
#     ('Tim Cook', 'is the CEO of', 'Apple'),
#
#     ('iOS', 'runs on', 'iPhone'),
#     ('macOS', 'runs on', 'iMac'),
#     ('watchOS', 'runs on', 'Apple Watch'),
#
#     ('Apple', 'was founded in', '1976'),
#     ('Apple', 'owns', 'App Store'),
#     ('App Store', 'sells', 'iOS apps'),
#
#     ('iPhone', 'announced in', '2007'),
#     ('iMac', 'announced in', '1998'),
#     ('Apple Watch', 'announced in', '2014'),
#     ('Vision Pro', 'announced in', '2023'),
# ]
# os.environ['OPENAI_API_KEY'] = "your-OpenAI-API-key"
# openai.api_key = os.environ['OPENAI_API_KEY']
# openai.api_key = "EMPTY"
# openai.api_base = "http://10.25.10.154:7860/"
# index_creator = GraphIndexCreator(llm=OpenAI(temperature=0))
#
# graph = index_creator.from_text('')
# for (node1, relation, node2) in kg:
#     graph.add_triple(KnowledgeTriple(node1, relation, node2))
#
# # Create directed graph
# G = nx.DiGraph()
# for node1, relation, node2 in kg:
#     G.add_edge(node1, node2, label=relation)
#
# # Plot the graph
# plt.figure(figsize=(25, 25), dpi=300)
# pos = nx.spring_layout(G, k=2, iterations=50, seed=0)
#
# nx.draw_networkx_nodes(G, pos, node_size=5000)
# nx.draw_networkx_edges(G, pos, edge_color='gray', edgelist=G.edges(), width=2)
# nx.draw_networkx_labels(G, pos, font_size=12)
# edge_labels = nx.get_edge_attributes(G, 'label')
# nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12)
#
# # Display the plot
# plt.axis('off')
# plt.show()

# import pandas as pd
# data = pd.read_excel('node_test1.xlsx')
# import networkx as nx
# G = nx.Graph()
# for i in range(len(data)):
#     node1 = data.iloc[i]['node1']
#     node2 = data.iloc[i]['node2']
#     relation = data.iloc[i]['relation']
#     # relation = 1
#     G.add_edge(node1, node2, label=relation)
# import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif'] = 'SimHei'
# # plt.figure(figsize=(10, 10))
# # nx.draw(G, with_labels=True)
# # plt.show()
#
# plt.figure(figsize=(10, 10))
# nx.draw(G, with_labels=True)
# edge_labels = nx.get_edge_attributes(G, 'label')
# nx.draw_networkx_edge_labels(G, pos=nx.spring_layout(G), edge_labels=edge_labels)
# plt.show()
# import pandas as pd
# data = pd.read_excel('node_test1.xlsx')
# import networkx as nx
# G = nx.DiGraph()  # 使用有向图
# for i in range(len(data)):
#     node1 = data.iloc[i]['node1']
#     node2 = data.iloc[i]['node2']
#     relation = data.iloc[i]['relation']
#     G.add_edge(node1, node2, label=relation, arrowstyle='->')  # 添加箭头样式
#
# import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif'] = 'SimHei'
# plt.figure(figsize=(10, 10))
# pos = nx.spring_layout(G)
# nx.draw(G, pos, with_labels=True, arrows=True)  # 设置arrows为True
# edge_labels = nx.get_edge_attributes(G, 'label')
# nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
# plt.show()

# import pandas as pd
# data = pd.read_excel('node_test1.xlsx')
# import networkx as nx
#
# G = nx.DiGraph()  # 使用有向图
#
# for i in range(len(data)):
#     node1 = data.iloc[i]['node1']
#     node2 = data.iloc[i]['node2']
#     relation = data.iloc[i]['relation']
#     G.add_edge(node1, node2, label=relation, arrowstyle='->')  # 添加箭头样式
#
# import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif'] = 'SimHei'
# plt.figure(figsize=(10, 10))
#
# # # 使用spring_layout布局，并添加一些参数来优化布局
# # pos = nx.spring_layout(G, k=0.3, iterations=50)
# #
# # nx.draw(G, pos, with_labels=True, arrows=True)  # 设置arrows为True
# #
# # # 调整边的标签位置以避免重叠
# # edge_labels = nx.get_edge_attributes(G, 'label')
# # edge_label_pos = nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels,
# #                                               label_pos=0.5, font_size=8, rotate=False)
# #
# # for _, label in edge_label_pos.items():
# #     label.set_bbox({'facecolor':'white', 'edgecolor':'none', 'alpha':0.7})
# #
# # plt.axis('off')  # 隐藏坐标轴
# # plt.tight_layout()  # 调整布局
# #
# # plt.show()
# # 使用Kamada-Kawai布局，并设置scale参数进行调整
# pos = nx.kamada_kawai_layout(G, scale=2)
#
# nx.draw(G, pos, with_labels=True, arrows=True)  # 设置arrows为True
#
# # 调整边的标签位置以避免重叠
# edge_labels = nx.get_edge_attributes(G, 'label')
# edge_label_pos = nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels,
#                                               label_pos=0.5, font_size=8, rotate=False)
# for _, label in edge_label_pos.items():
#     label.set_bbox({'facecolor':'white', 'edgecolor':'none', 'alpha':0.7})
#
# plt.axis('off')  # 隐藏坐标轴
# plt.tight_layout()  # 调整布局
#
# plt.show()

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
data = pd.read_excel('node_test1.xlsx')
G = nx.DiGraph()
for i in range(len(data)):
    node1 = data.iloc[i]['node1']
    node2 = data.iloc[i]['node2']
    relation = data.iloc[i]['relation']
    G.add_edge(node1, node2, label=relation, arrowstyle='->')
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.figure(figsize=(10, 10))
# 使用Kamada-Kawai布局，并设置scale参数进行调整
pos = nx.kamada_kawai_layout(G)
# 调整位置相近但没有关系的两个节点之间的距离
fixed_positions = {node: (pos[node][0], pos[node][1] + 0.2) if node.startswith('A') else pos[node] for node in G.nodes}
pos = nx.spring_layout(G, pos=fixed_positions)
nx.draw(G, pos, with_labels=True, arrows=True)  # 设置arrows为True
# 调整边的标签位置以避免重叠
edge_labels = nx.get_edge_attributes(G, 'label')
edge_label_pos = nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels,
                                              label_pos=0.5, font_size=8, rotate=False)
for _, label in edge_label_pos.items():
    label.set_bbox({'facecolor': 'white', 'edgecolor': 'none', 'alpha': 0.7})
plt.axis('off')  # 隐藏坐标轴
plt.tight_layout()  # 调整布局
plt.show()
